package com.aaa.javaweb.dao.impl;

import com.aaa.javaweb.dao.StaffDao;
import com.aaa.javaweb.entity.Staff;
import com.aaa.javaweb.until.JDBCUntil;

import java.util.List;
import java.util.Map;

public class StaffDaoimpl implements StaffDao {


    @Override
    public List<Map> selectALL(int page, int limit) {
        String sql = "select t_staff.id,t_staff.sname,t_staff.rzdate,t_staff.address,t_staff.status,t_staff.imgpath,t_juese.jname,t_departtable.dname from t_staff LEFT JOIN  t_juese on t_staff.jueseid=t_juese.id join t_departtable on t_departtable.id=t_staff.deptid limit ?,?";
        int start = (page-1)*limit;
        return JDBCUntil.selectMap(sql,start,limit);
    }

    @Override
    public List<Map> seljuese() {
        String sql ="select *  from t_juese";
        return JDBCUntil.selectMap(sql);
    }

    @Override
    public int updone(Staff staff) {
        if (staff.getId()==null){
            String sql ="insert into t_staff (sname,rzdate,address,status,imgpath,jueseid,deptid) values(?,?,?,?,?,?,?)";
            return JDBCUntil.Updateout(sql,staff.getSname(),staff.getRzdate(),staff.getAddress(),staff.getStatus(),staff.getImgpath(),staff.getJueseid(),staff.getDeptid());
        }else {
            String sql ="update t_staff set sname=?,rzdate,address=?,status=?,imgpath=?,jueseid=?,deptid=? where id=?";
            return JDBCUntil.Updateout(sql,staff.getSname(),staff.getRzdate(),staff.getAddress(),staff.getStatus(),staff.getImgpath(),staff.getJueseid(),staff.getDeptid(),staff.getId());
        }

    }

    /**
     * 查询部门里的所有dname
     * @return
     */
    @Override
    public List<Map> selbumen() {
        String sql ="select * from t_departtable";
        return JDBCUntil.selectMap(sql);
    }

    @Override
    public int delone(String gid) {
        String sql = "delete from t_staff where id=?";
        return JDBCUntil.Updateout(sql,gid);
    }

    @Override
    public int selcount() {
        String sql="select count(*) as a from t_staff LEFT JOIN  t_juese on t_staff.jueseid=t_juese.id join t_departtable on t_departtable.id=t_staff.deptid";
        return Integer.parseInt(JDBCUntil.selectMap(sql).get(0).get("a")+"");
    }
}
